package com.hjq.controller;

import com.hjq.common.PageResult;
import com.hjq.common.Result;
import com.hjq.entity.po.Course;
import com.hjq.entity.vo.CourseDetailVO;
import com.hjq.entity.vo.CourseSearchVO;
import com.hjq.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 课程管理
 **/
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 条件查询
     *
     * @param courseSearchVO 查询条件
     * @return List<Object>
     */
    @GetMapping("/search")
    public Result<PageResult<Course>> findByCondition(CourseSearchVO courseSearchVO) {
        log.info("课程查询条件：{}", courseSearchVO);
        PageResult<Course> result = courseService.findByCondition(courseSearchVO);
        log.info("课程搜索结果,{}",result);
        return Result.success(result);
    }

    /**
     * 根据ID查询课程详情
     *
     * @param id 课程ID
     * @return List<CourseDetailVO>
     */
    @GetMapping("/detail/{id}")
    public Result<CourseDetailVO> findDetailById(@PathVariable Long id) {
        log.info("课程ID：{}", id);
        CourseDetailVO list = courseService.findDetailById(id);
        return Result.success(list);
    }

    /**
     * 删除课程
     *
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Object> deleteById(@PathVariable Integer id) {
        Integer i = courseService.deleteById(id);
        if (i == 1)
            return Result.success("删除成功");
        return Result.error("删除失败");
    }

    /**
     * 修改课程
     *
     * @param course 课程信息
     * @return 修改结果
     */
    @PutMapping
    public Result<Object> update(@RequestBody Course course) {
        Integer i = courseService.update(course);
        return i == 1 ? Result.success("修改成功") : Result.error("修改失败");
    }
}
